
// import Hybrid from '@/components/uzone-hybrid';

export default {
  name: 'coupon-list',
  data() {
    return {
      couponList: [], // 优惠券列表
    };
  },

  methods: {
    // 获取优惠券列表
    getUserTicketList() {
      const params = {
        page: {
          pageNo: 1,
          pageSize: 1000,
        }
      };
      this.$api.getUserTicketList(params).then(res => {
        const { status, data } = res;
        if ('1' === status && data) {
          this.couponList = data.resultData;
          /* this.couponList = [
            { useStatus: '0', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
            { useStatus: '1', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
            { useStatus: '2', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
            { useStatus: '3', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
            { useStatus: '4', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
            { useStatus: '5', discountAmount: 2000, discountCouponType: '1', dueTime: '2021-09-01' },
          ];*/
        }
        if (this.couponList.length === 0) {
          const body = document.querySelector('body');
          body.className = body.className + 'no_coupon';
        }
      });
    },
    // 转换优惠券状态
    couponStatusCls(status) {
      // 优惠券使用状态 0未使用 1已过期 2使用中 3已使用 4已失效
      switch (status) {
        case '0':
          return 'Unused';
        case '1':
          return 'overdue';
        case '2':
          return 'using';
        case '3':
          return 'used';
        case '4':
          return 'invalid';
        default:
          return '';
      }
    },
    // 使用优惠券
    useCoupon(coupon) {
      if (coupon.useStatus !== '0') return; // 如果不是未使用的优惠券，则不处理
      if (coupon.discountCouponType === '1') {
        // 如果是还款优惠券
        // 查询接口获取跳转位置
        this.$api.jumpToWhere({}).then(res => {
          const { status, data, msg } = res;
          if ('1' === status && data) {
            if (data.flag === '2') {
              // 跳转至还款页面
              const query = this.$route.query;
              query.orderId = data.orderId;
              this.$router.push({
                path: '/repay',
                query,
              });
            } else if (data.flag === '1') {
              // 跳转至首页
              this.gotoHome();
            }
          } else {
            this.$toast(msg || 'The coupon is available after successful loan');
          }
        });
      } else {
        // 如果是其他优惠券则跳转首页
        this.gotoHome();
      }
    },
    // 返回APP首页
    gotoHome() {
      // const hybrid = new Hybrid(this.$os.android ? 'android' : 'ios');
      this.$hybrid.action('gotoPage', { pageId: 'home' }); // 返回首页
    }
  },

  filters: {
    couponType(val) {
      switch (val) {
        case '1':
          return 'Repayment coupon'; // 还款优惠券
        case '2':
          return 'Interest-free coupons'; // 免息优惠券
        default:
          return '';
      }
    }
  },

  created() {
    this.getUserTicketList();
  }

};
